package com.meisiabc.support.wechat;

import com.meisiabc.support.wechat.fix.FixTemplates;
import com.riversoft.weixin.common.media.Media;
import com.riversoft.weixin.common.media.MediaType;
import com.riversoft.weixin.common.menu.Menu;
import com.riversoft.weixin.common.menu.MenuItem;
import com.riversoft.weixin.common.menu.MenuType;
import com.riversoft.weixin.common.oauth2.AccessToken;
import com.riversoft.weixin.mp.MpWxClientFactory;
import com.riversoft.weixin.mp.base.AppSetting;
import com.riversoft.weixin.mp.care.CareMessages;
import com.riversoft.weixin.mp.media.Medias;
import com.riversoft.weixin.mp.menu.Menus;
import com.riversoft.weixin.mp.oauth2.MpOAuth2s;
import com.riversoft.weixin.mp.template.Data;
import com.riversoft.weixin.mp.user.Users;
import com.riversoft.weixin.mp.user.bean.User;
import lombok.extern.slf4j.Slf4j;

import java.io.InputStream;
import java.util.Map;

@Slf4j
public class WechatMpClient {
    private AppSetting appSetting;

    public WechatMpClient() {
        this.appSetting = new AppSetting("wx0c8ec854eb3dc446", "11ff9c21c57f199e347b90677df1a96b");
    }

    public String authenticationUrl(String redirect, String scope) {
        return MpOAuth2s.with(this.appSetting).authenticationUrl(redirect, scope);
    }

    public AccessToken getAccessToken(String code) {
        return MpOAuth2s.with(this.appSetting).getAccessToken(code);
    }


    public void sendTemplateMsg(String openid, String templateId, Map<String, Data> messages) {
        sendTemplateMsg(openid, templateId, null, messages);
    }

    public void sendTemplateMsg(String openid, String templateId, String url, Map<String, Data> messages) {
        FixTemplates.with(this.appSetting).sendFix(openid, templateId, url, null, messages);
    }

    public User getUsers(String openId) {
        return Users.with(appSetting).get(openId);
    }

    public void menuInit() {
        Menu menu = new Menu();
        MenuItem m1 = new MenuItem();
        m1.setName("免费预约");
        m1.setType(MenuType.miniprogram);
        m1.setUrl("http://mp.weixin.qq.com");
        m1.setAppId("wxd10308eaab66e9b4");
        m1.setPagePath("pages/bespeak/bespeak");
        menu.add(m1);

        MenuItem m2 = new MenuItem();
        m2.setName("课程体系");
        m2.setType(MenuType.miniprogram);
        m2.setUrl("http://mp.weixin.qq.com");
        m2.setAppId("wxd10308eaab66e9b4");
        m2.setPagePath("pages/system/system");
        menu.add(m2);


        MenuItem m3 = new MenuItem();
        m3.setName("我的美思");
        m3.setType(MenuType.miniprogram);
        m3.setUrl("http://mp.weixin.qq.com");
        m3.setAppId("wxd10308eaab66e9b4");
        m3.setPagePath("pages/my/my");

        MenuItem m4 = new MenuItem();
        m4.setName("我要推荐");
        m4.setType(MenuType.click);
        m4.setKey("recommender");

        MenuItem mp3 = new MenuItem();
        mp3.setName("我的美思");
        mp3.add(m3);
        mp3.add(m4);

        menu.add(mp3);

        Menus.with(this.appSetting).create(menu);
    }

    public Media uploadMedias(MediaType type, InputStream inputStream, String fileName) {
        try {
            return Medias.with(this.appSetting).upload(type, inputStream, fileName);
        } catch (Exception e) {
            log.error("", e);
        }

        return null;
    }

    public void careMessagesForImage(String openId, String image) {
        try {
            CareMessages.with(this.appSetting).image(openId, image);
        } catch (Exception e) {
            log.error("", e);
        }
    }
}
